untyped constant(型付けなし定数)
untyped constant(型付けなし定数)
untyped constantはconstant(定数)の一種である。
constant(定数)は次の2種類に分けられる。
typed constant
untyped constant
次のものはuntyped constantである:
定数リテラル。例: 123 1.23 "Hello, World!"
定数リテラル = integerリテラル、runeリテラル、floating-pointリテラル、imaginaryリテラル、stringリテラル
true false (事前宣言された識別子であり定数)
iota
ある種のconstant expressions(定数式)であって、そのoperandとしてuntyped constantだけを含むもの。
constantには、明示的に型を与えることができる。具体的には次のような場面である:
constant declaration(定数宣言) const s string = "Hello, World"
conversion(型変換) const s = string("Hello, World")
variable declaration(変数宣言) var s string = "Hello, World"
assignment statement(代入文) var s string; s = "Hello, World"
式のoperandとして現れる場合 var s string = "Hello"; fmt.Println(s + ", World")
untyped constantは、それぞれのdefault type(デフォルト型)を持っている。
型の付いた値が必要となる場所でuntyped constantを用いた場合、暗黙的にdefault typeへの変換が行われる。
例: short variable declaration(短縮変数宣言)
a := 1と書いたとき、変数aは型を必要とするが1はuntyped integerなので、そのdefault typeであるintに変換される。
仕様書の該当箇所: https://golang.org/ref/spec#Constants